<?php

class IndexAction extends CommonAction {

    	public function index()
	{
//            dump($_SESSION);
            $pdocs = M('Pdoc')->where('pdoc_isdel=0')->order('pdoc_createtime desc')->limit(10)->select();
            
            $db_m_membersPdoc = M('MMembersPdoc');
            $uid = $_SESSION['current_member']['uid'];
                
            for($i = 0; $i<count($pdocs); $i++){
                $pdocs[$i]['time_study'] = $db_m_membersPdoc->where(array('uid'=>$uid, 'pdoc_id'=>$pdocs[$i]['pdoc_id']))->sum('time_study');
            }
            
            $this->assign('pdocs', $pdocs);
            
            $this->display();
            

	}

	public function index_bak()
	{
                $db_m_membersPdoc = M('MMembersPdoc');
		$pdocs = M('Pdoc')->where('pdoc_isdel=0')->order('pdoc_createtime desc')->limit(10)->select();
                $uid = $_SESSION['current_member']['uid'];
                
                for($i = 0; $i<count($pdocs); $i++){
                    $pdocs[$i]['time_study'] = $db_m_membersPdoc->where(array('uid'=>$uid, 'pdoc_id'=>$pdocs[$i]['pdoc_id']))->getField('time_study');
                }
                //dump($_SESSION);
		$this->assign('pdocs', $pdocs);
		$this->display();

	}

	public function saveExit()
	{
            session_unset();
            session_destroy();
            $this->redirect('Login/index');
	}
        
        public function changePwd(){
            
            $oldPwd = I('old_pwd', '');
            $newPwd = I('new_pwd', '');
            $username = I('username', '');
            
            $db_members = M('Members');
            
            $res = $db_members->where(array("username"=>$username, "password"=>md5($oldPwd)))->find();
            if($res){
                $db_members-> where(array("username"=>$username))->setField('password',md5($newPwd));
                $this->ajaxReturn(array('status' => 1, 'info' => '修改成功！', 'data' => null, 'jumpUrl' => null ));
            }else{
                $this->ajaxReturn(array('status' => 0, 'info' => '旧密码错误！', 'data' => null, 'jumpUrl' => null ));
            }
         
        }

}